package com.szqhyxj.youxiangju.biz.merchantHome.plan.adapter;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.plan.PlanModel;
import com.szqhyxj.youxiangju.biz.plan.PlanTaskStatus;
import com.szqhyxj.youxiangju.biz.plan.TaskType;
import com.szqhyxj.youxiangju.biz.plan.TasksItem;
import com.szqhyxj.youxiangju.sys.util.DataUtils;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindColor;
import butterknife.BindDrawable;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * XiaoPanYu
 * on 2018/11/8
 */
public class PlanParticularsAdapter extends BaseExpandableListAdapter {

    private List<List<Map<String, TasksItem>>> tasksList;
    private List<PlanModel.PlanParticularsDayEntity> entityList;

    public PlanParticularsAdapter(List<List<Map<String, TasksItem>>> tasksList, List<PlanModel.PlanParticularsDayEntity> entityList) {
        this.tasksList = tasksList;
        this.entityList = entityList;

    }

    @Override
    public int getGroupCount() {
        return entityList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return tasksList.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return entityList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return tasksList.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }



    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.expandable_list_plan_group_view, null);
            GroupHolder groupHolder = new GroupHolder(view);
            view.setTag(groupHolder);
        }

        GroupHolder groupHolder = (GroupHolder) view.getTag();
        PlanModel.PlanParticularsDayEntity entity = entityList.get(i);
        String time = DataUtils.timeStamp2Day(entity.getTime());
        groupHolder.time.setText(time);
        groupHolder.context.setText(String.format(Locale.getDefault(), "已消费%d笔 已还款%d笔", entity.getExpenditureNum(), entity.getRefundNum()));

        if (b) {
            groupHolder.arrow.setImageDrawable(groupHolder.arrowDown);
        } else {
            groupHolder.arrow.setImageDrawable(groupHolder.arrowRight);
        }

        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.expandable_list_plan_child_view, null);
            ChildHolder childHolder = new ChildHolder(view);
            view.setTag(childHolder);
        }

        ChildHolder childHolder = (ChildHolder) view.getTag();
        Map<String, TasksItem> tasks = tasksList.get(i).get(i1);

        TasksItem consumeBean = tasks.get(PlanModel.CONSUME);
        TasksItem refundBean = tasks.get(PlanModel.REFUND);
        //消费
        if(consumeBean!=null){
            childHolder.consume.setVisibility(View.VISIBLE);
            setContent(childHolder,childHolder.consumeType, childHolder.consumePrice, childHolder.consumeTime, consumeBean);
        }else {
            childHolder.consume.setVisibility(View.INVISIBLE);
        }

        //还款
        if(refundBean!=null){
            childHolder.repayment.setVisibility(View.VISIBLE);
            setContent(childHolder,childHolder.repaymentType, childHolder.repaymentPrice, childHolder.repaymentTime, refundBean);
        }else {
            childHolder.repayment.setVisibility(View.GONE);
        }

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    static class ChildHolder {
        @BindView(R.id.consume_type)
        TextView consumeType;
        @BindView(R.id.consume_price)
        TextView consumePrice;
        @BindView(R.id.consume_time)
        TextView consumeTime;
        @BindView(R.id.consume)
        LinearLayout consume;
        @BindView(R.id.repayment_type)
        TextView repaymentType;
        @BindView(R.id.repayment_price)
        TextView repaymentPrice;
        @BindView(R.id.repayment_time)
        TextView repaymentTime;
        @BindView(R.id.repayment)
        LinearLayout repayment;
        @BindColor(R.color.colorTextBlue)
        int blue;
        @BindColor(R.color.textcolor4e)
        int eeee;
        @BindColor(R.color.colorTextGray)
        int gray;

        ChildHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class GroupHolder {
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.context)
        TextView context;
        @BindView(R.id.arrow)
        ImageView arrow;
        @BindDrawable(R.mipmap.jiantou_xia_h)
        Drawable arrowDown;

        @BindDrawable(R.mipmap.jiantou_you_h)
        Drawable arrowRight;

        GroupHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private void setContent(ChildHolder holder,TextView type, TextView price, TextView time, TasksItem bean) {

        String title;
        if (bean.getType() == TaskType.PAY.getCode()) {
            title = TaskType.PAY.getDesc();
        } else {
            title = TaskType.REPAY.getDesc();
        }

        String statusTxt;

        if (bean.getStatus() == PlanTaskStatus.SUCCESS.getCode()) {// 0 预览 1 就绪 2 执行 3 失败 4 成功
            type.setTextColor(holder.blue);
            price.setTextColor(holder.blue);
            time.setTextColor(holder.blue);
            statusTxt = title + "成功";
        } else if (bean.getStatus() == PlanTaskStatus.FAIL.getCode()) {
            type.setTextColor(holder.eeee);
            price.setTextColor(holder.eeee);
            time.setTextColor(holder.eeee);
            statusTxt = title + "失败";
        } else {
            type.setTextColor(holder.gray);
            price.setTextColor(holder.gray);
            time.setTextColor(holder.gray);
            statusTxt = "未" + title;
        }

        type.setText(statusTxt);
        price.setText(String.format(Locale.getDefault(), "%.2f",  bean.getAmount())+"元");
        time.setText(DataUtils.timeStamp2Hour(bean.getToExecuteTime()));
    }
}